package com.gouaixin.bee.beeframework2.extra.sms;

import static cn.smssdk.framework.utils.R.getStringRes;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gouaixin.bee.beeframework2.R;

public class SmsDemoActivity extends Activity implements OnClickListener {
    Context context=SmsDemoActivity.this;

    private Button sensmsButton,verificationButton,countryButton;
    private TextView countryTextView,textView2;
    private EditText phonEditText,verEditText;
    // 填写从短信SDK应用后台注册得到的APPKEY
    private static String APPKEY = "732340ec6b34";//463db7238681  27fe7909f8e8
    // 填写从短信SDK应用后台注册得到的APPSECRET
    private static String APPSECRET = "d03a78efdae0b38ef9e50ccace5d48ac";//
    public String phString;                                              //3684fd4f0e16d68f69645af1c7f8f5bd
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smsdemo);
        sensmsButton=(Button) findViewById(R.id.button1);
        verificationButton=(Button) findViewById(R.id.button2);
        countryButton=(Button) findViewById(R.id.button3);
        countryTextView=(TextView) findViewById(R.id.textView1);
        textView2=(TextView) findViewById(R.id.textView2);
        phonEditText=(EditText) findViewById(R.id.editText1);
        verEditText=(EditText) findViewById(R.id.editText2);
        sensmsButton.setOnClickListener(this);
        verificationButton.setOnClickListener(this);
        countryButton.setOnClickListener(this);

        //System.loadLibrary("OSbase");
        SMSSDK.initSDK(this,APPKEY,APPSECRET);
        EventHandler eh=new EventHandler(){

            @Override
            public void afterEvent(int event, int result, Object data) {

                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                handler.sendMessage(msg);
            }

        };
        SMSSDK.registerEventHandler(eh);

//        initIntent();
    }

    public final static  int getCode=0;//获取验证码
    public final static  int confirmCode=1;//确认验证码

    private void initIntent() {
       Intent it=getIntent();
       int flag=it.getIntExtra("flag",0);
        String phone=it.getStringExtra("phone");
        String ver=it.getStringExtra("ver");
        switch (flag){
            case SmsDemoActivity.getCode:
                onGetCode(phone);
                break;

            case SmsDemoActivity.confirmCode:
                onConfirmCode(phone,ver);
                break;
        }

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String phone=phonEditText.getText().toString();
        String ver=verEditText.getText().toString();
        switch (v.getId()) {
            case R.id.button1://获取验证码


                onGetCode(phone);


                break;
            case R.id.button2://校验验证码

                onConfirmCode(phone,ver);

                break;
            case R.id.button3://国家列表
                SMSSDK.getSupportedCountries();
                //SMSSDK.getCountry(arg0);
                SMSSDK.getGroupedCountryList();
                //SMSSDK.getCountryByMCC(arg0);
                break;
            default:
                break;
        }
    }

    //校验验证码
    private void onConfirmCode(String phone,String ver) {
        if(!TextUtils.isEmpty(ver)){
            SMSSDK.submitVerificationCode("86", phone, ver);
        }else {
            Toast.makeText(this, "电话或验证码不能为空", Toast.LENGTH_SHORT).show();
        }

    }

    //获取验证码
    private void onGetCode(String phone) {

        if(!TextUtils.isEmpty(phone)){
            SMSSDK.getVerificationCode("86",phonEditText.getText().toString());
        }else {
            Toast.makeText(this, "电话不能为空", Toast.LENGTH_SHORT).show();
        }

    }

    Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            int event = msg.arg1;
            int result = msg.arg2;
            Object data = msg.obj;
            Log.e("event", "event="+event);
            if (result == SMSSDK.RESULT_COMPLETE) {
                //短信注册成功后，返回SmsDemoActivity,然后提示新好友
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交验证码成功
                    Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();
                    textView2.setText("提交验证码成功");
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                    Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
                    textView2.setText("验证码已经发送");
                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){//返回支持发送验证码的国家列表
                    Toast.makeText(getApplicationContext(), "获取国家列表成功", Toast.LENGTH_SHORT).show();
                    countryTextView.setText(data.toString());

                }
            } else {
                ((Throwable) data).printStackTrace();
                int resId = getStringRes(SmsDemoActivity.this, "smssdk_network_error");
                Toast.makeText(SmsDemoActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                if (resId > 0) {
                    Toast.makeText(SmsDemoActivity.this, resId, Toast.LENGTH_SHORT).show();
                }
            }

        }

    };
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        SMSSDK.unregisterAllEventHandler();
    }


}
